package demo3;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPoolExecutor {
    //用来组织任务的队列
    private BlockingQueue<Runnable> queue;

    public MyThreadPoolExecutor(int capacity,int queueCapacity){
        if (capacity<0) throw new IllegalArgumentException("线程参数必须大于0");
        if(queueCapacity<0) throw new IllegalArgumentException("阻塞队列大小必须大于0");
        queue=new LinkedBlockingQueue<>(queueCapacity);
        for (int i = 0; i < capacity; i++) {
            Thread t1 =new Thread(()->{
                while (true){
                    try {
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        if (runnable==null) throw new IllegalArgumentException("任务不能为空");
        queue.put(runnable);
    }
}
